#!/usr/bin/env ruby -w
#--
# Project:      pkgman
# File:         test/make_pkgman_pkg.rb
# Revision:     $Id$
# Home:         http://code.google.com/p/pkgman/
# Author:       Derek Olsen <someword at gmail dot com>
# Copyright:    (c) 2007 by Derek Olsen
# License:      GNU General Public License v2 
#++

# This script is used to create a solaris pkgman package

require 'ftools'

# Name of the script we are packaging
app_name = "pkgman.rb"

# Define where the pkgman script lives
app_dir = File.join(File.dirname(__FILE__), "../", "bin")
pkg_dir = File.join(File.dirname(__FILE__), "/", "pkgs")
 
# Base the version number off of the svn revision number for the time being
app_version = ""

File.open("#{app_dir}/#{app_name}").each do |l|
  if l =~ /\$Id:/
    app_version = l.split(" ")[4]
  end
end
#

# Define the name of the pkg filename
pkg_file = "pkgman-#{app_version}-sol10-any"

# pkgman pkginfo contents
pkginfo_contents=<<_EOF
PKG="pkgman"
NAME="Solaris Package Management" 
ARCH="all"
VERSION="#{app_version}"
CATEGORY="application"
VENDOR="http://code.google.com/p/pkgman/"
EMAIL="someword@gmail.com"
PSTAMP="Derek Olsen"
BASEDIR="/usr/local"
CLASSES="none"
_EOF

# pkgman prototype contents
prototype_contents=<<_EOF
d none bin 0755 root root
f none bin/pkgman.rb 0755 root root
i pkginfo 
_EOF
      
# Create the pkgs directory if it doesnt exist
Dir.mkdir(pkg_dir) unless File.directory?(pkg_dir)

# Make a working directory
workdir = "/var/tmp/" + File.basename($0) + "." + Process.pid.to_s
Dir.mkdir(workdir)

# Write out the pkginfo file
f = File.open("#{workdir}/pkginfo", "w+")
f << pkginfo_contents
f.close

# Write out the prototype file
f = File.open("#{workdir}/prototype", "w+")
f << prototype_contents
f.close

# Make the application directory
Dir.mkdir("#{workdir}/bin")

# Put the script into the pkg directory
File.copy("#{app_dir}/#{app_name}", "#{workdir}/bin/#{app_name}")


# Create the pkg
system("/usr/bin/pkgmk -o -b #{workdir} -f #{workdir}/prototype 2>/dev/null")

# Translate the pkg into datastream
system("/usr/bin/pkgtrans -o -s /var/spool/pkg #{File.expand_path(pkg_dir)}/#{pkg_file} pkgman 2>/dev/null")

puts "You'll find the pkg at #{File.expand_path(pkg_dir)}/#{pkg_file}"
